import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhit.Dao.StudentDao;
import com.qhit.pojo.Student;
import com.qhit.untils.MyBatisUntil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.apache.ibatis.io.Resources;


import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.util.List;

import static org.junit.Assert.*;

/**
 * Description: NewSsm
 * Created by WuHuaSen .
 * Created Date:  2022/3/29 20:56
 * Version:  V1.0
 */
public class StudentDaoTest {
    MyBatisUntil myBatisUntil = new MyBatisUntil();
    @Test
    public void insertStudent() throws Exception {
        SqlSession sqlSession=  myBatisUntil.getSqlSession(false);
        StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
        //测试StudentDao方法
        int i = studentDao.InsertStudent(new Student(2, "1001", "张三", "男", 21));
        //手动提交，一般会有多个操作
        sqlSession.commit();
        //操作2
        //操作三

        System.out.println(i);
    }

    @Test
    public void deleteStudent() throws Exception {
        try {
            //自动提交
            StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
            int i = studentDao.DeleteStudent(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void updateStudent() {
        StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
        int i = studentDao.updataStudent(new Student(0, "2222", "1wangwu", "女", 21));
        myBatisUntil.getSqlSession(true);
        assertEquals(1, i);
    }

    @Test
    public void SelectStudent() {
        StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
        PageHelper.startPage(1,4);
        List<Student> students = studentDao.SelectStudent();
        PageInfo<Student> pageInfo = new PageInfo<Student>(students);
        List<Student> list =pageInfo.getList();
        for (Student stu:list
             ) {
            System.out.println(stu);
        }

    }

    //根据主键查询Student信息
    @Test
    public void SelectStudentById() {
        StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
        Student student = studentDao.SelectStudentById(2);
        System.out.println(student);
    }

    //分页查询
    @Test
    public void SelectStudentByLimit() {
        StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
        List<Student> ListStudent = studentDao.SelectStudentByLimit(0,3);
        assertNotNull(ListStudent);
        for (Student stu : ListStudent
                ) {
            System.out.println(stu);
        }
    }
    //添加操作，主键回填
    @Test
    public void  InsertStudentReturnId(){
        StudentDao studentDao = myBatisUntil.getMapper(StudentDao.class);
        //测试StudentDao方法
        int i = studentDao.InsertStudent(new Student(3, "1001", "张三", "男", 21));
        //自动提交
        myBatisUntil.getSqlSession(true);
    }
}